package com.zc.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zc.service.UserService;
import com.zc.pojo.*;
import com.zc.util.Page;
import com.zc.util.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
/**
*  加入了注释 4
*/
/**
*  加入了注释 5
*/

/**
 * 加入了注释 j
 */
@Controller
@RequestMapping("demo/user")
public class UserController {
    /**
     * 新的注释
     */
    @Autowired
    UserService userService;

    /**
     * 新的注释2
     */
    @RequestMapping("userPage")
    @ResponseBody
    public String userPage(User user, @RequestParam("pageNo")Integer pageNo){

        Integer count = userService.countUser(user);//查询总条数

        Page page =PageUtils.getPage(3,count,pageNo);
        List<User> list = userService.userPage(page,user);
        page.setData(list);//需要将数据集合放到 page对象中


        return JSONObject.toJSONString(page);
    }

    /**
     * 新的注释3
     */
    @RequestMapping("delDept")
    public String  delDept(Dept dept){

        userService.delDept(dept);
        return "redirect:/demo/dept_index.html";
    }




    @RequestMapping("one")
    @ResponseBody
    public String one(User user){
        User us = userService.one(user);
        List<Dept> list = userService.dept();
        JSONObject object = new JSONObject();
        object.put("user",us);
        object.put("list",list);
        return JSONObject.toJSONString(object);
    }


    @RequestMapping("queryDept")
    @ResponseBody
    public String queryDept(Dept dept){
        List<Dept> list = userService.queryDept(dept);
        return JSONArray.toJSONString(list);
    }

    @RequestMapping("query")
    @ResponseBody
    public String query(User user){
        List<User> list = userService.query(user);
       return JSONArray.toJSONString(list);
    }

    @RequestMapping("dept")
    @ResponseBody
    public String dept(){
        List<Dept> list = userService.dept();
        return JSONArray.toJSONString(list);
    }

    @RequestMapping("add")
    @ResponseBody
    public String add(User user){
        if(user.getId()!=null && !user.getId().equals("")){
            userService.update(user);
        }else{
            userService.add(user);
        }

        return "1";
    }

}
